<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>排放外观检验项目</title>
    <link rel="stylesheet" type="text/css" href="../../css/api.css" />
    <link rel="stylesheet" type="text/css" href="../../css/style.css" />
    <link rel="stylesheet" type="text/css" href="../../css/weui.min.css">
    <script type="text/javascript" src="../../script/jquery-1.10.2.min.js"></script>
</head>
<body>
    <div class="container" id="container">
        <div id="divList">
        </div>
            <div class="weui_cell">
                <div class="weui_cell_hd"><label class="weui_label">检验结果</label></div>
                    <div class="weui_cell_bd weui_cell_primary">
                    <select class="select" style="width: 200px" id="selectResult">
                        <option value="1">合格</option>
                        <option value="2">不合格</option>
                    </select>
                </div>
            </div>
             <div class="weui_cell">
                <div class="weui_cell_hd"><label class="weui_label">检测线代号:</label></div>
                    <div class="weui_cell_bd weui_cell_primary">
                        <select class="select" style="width: 150px" id="selectLineNum">
                            <option selected="selected" value="1">1</option>
                            <option value="2">2</option>
                            <option value="3">3</option>
                            <option value="4">4</option>
                            <option value="5">5</option>
                            <option value="6">6</option>
                            <option value="7">7</option>
                            <option value="8">8</option>
                            <option value="9">9</option>
                            <option value="10">10</option>
                            <option value="11">11</option>
                            <option value="12">12</option>
                            <option value="13">13</option>
                            <option value="14">14</option>
                            <option value="15">15</option>
                        </select>
                    </div>
            </div>
            <div class="weui_cells_title">项目为是的汇总</div>
            <div class="weui_cell">
                 <span id="spItemsY" style="color:red"></span>
            </div> 
            <div class="weui_cells_title">项目为否的汇总</div>
            <div class="weui_cell">
                 <span id="spItemsN" style="color:red"></span>
            </div>
        </div> 
    <div class="mainbutton">
        <a class="weui_btn weui_btn_default" href="javascript:;" id="btnSaveWJ" onclick="SaveWJ()">提交外检</a>            
        </div>   
    </div>
    <div class="space">
        
    </div>
</body>
</html>
<script type="text/javascript" src="../../script/api.js"></script>
<script type="text/javascript">
    var chk_value_Y = [];
    var chk_value_N = [];
    apiready = function(){
        var jsfun = "SetTitle('检验项目列表');";
        api.execScript({
            name: 'win_wgzplist',
            script: jsfun
        });

        var vLineNum = $api.getStorage('LineNum');
        if(vLineNum != null)
        {
            $("#selectLineNum").val(vLineNum);
        }

        GetWJJYXM(api.pageParam.ID);
    };

    function GetWJJYXM(paraID)
    {
         var vUrlRead = $api.getStorage('ServerUrlRead');
         api.ajax({
            url:vUrlRead + '/WcfServices/GetWJAllJYXM_PF/' + paraID,
            method: 'post',
            contentType: "application/json",
        }, function(ret, err) {
            if (ret.IsSuccess) {
                var vData = InitJYXMList(ret.Data);
                
                $("#btnSaveWJ").removeClass("weui_btn_default");
                $("#btnSaveWJ").addClass("weui_btn_primary");
                ChangedItems();
            }
        });
    }

    function InitJYXMList(data)
    {
        var lstData = "<div class=\"weui_cells weui_cells_checkbox\">";
        for (var i = 0; i < data.length; i++) {
            lstData += "<label class=\"weui_cell weui_check_label\" for=\"chkLst" + data[i].ID + "\" >";
            lstData += " <div class=\"weui_cell_bd weui_cell_primary\">";
            lstData += "   <p>" + data[i].SortNum + "、" + data[i].JYXM + "</p>";
            lstData += " </div></label>";
            lstData += " <div style='width:100%; text-align:center'>";

            if(data[i].IsEnable == "1")
            {
                var indexY = $.inArray(data[i].SortNum, chk_value_Y); 
                if (indexY < 0) {
                    chk_value_Y.push(data[i].SortNum.toString());
                }
                lstData += " <label><input name='Pass"+ data[i].SortNum +"' checked=\"checked\" type=\"radio\" value='"+data[i].SortNum+"' onclick='AddY(this);'/>是</label> ";
                lstData += " <label><input name='Pass"+ data[i].SortNum +"' type=\"radio\" value='"+data[i].SortNum+"' onclick='AddN(this);' />否</label>";
            }
            else
            {
                var indexN = $.inArray(data[i].SortNum, chk_value_N); 
                if (indexN < 0) {
                    chk_value_N.push(data[i].SortNum.toString());
                }
                lstData += " <label><input name='Pass"+ data[i].SortNum +"' type=\"radio\" value='"+data[i].SortNum+"' onclick='AddY(this);'/>是</label> ";
                lstData += " <label><input name='Pass"+ data[i].SortNum +"'  checked=\"checked\" type=\"radio\" value='"+data[i].SortNum+"' onclick='AddN(this);' />否</label>";               
            }

            lstData += " </div> ";
        }

        lstData += "</div>";
        $("#divList").append(lstData);
    }

    function SaveWJ()
    {
        var vClass = $("#btnSaveWJ").attr("class");
        var array=vClass.split(" "); 

        var vEnable = false;
        for(var i = 0;i<array.length;i++)
        {
            if(array[i] == "weui_btn_primary")
            {
                vEnable = true;
                break;
            }
        }

        if(vEnable)
        {   
            $("#btnSaveWJ").removeClass("weui_btn_primary");
            $("#btnSaveWJ").addClass("weui_btn_default");

            var vId = api.pageParam.ID;
            var vJYY =  $api.getStorage('UserCHName');
            var vJYYSFZH =  $api.getStorage('UserSFZH');
            var vJCXDH = $("#selectLineNum").val();
            var vWGY = $("#spItemsY").html();
            var vWGN = $("#spItemsN").html();
            var vWGJG = $("#selectResult").val();

            var vUrlWrite = $api.getStorage('ServerUrlWrite');

            $.ajax({
                type: 'Post',
                url: vUrlWrite + '/WcfServices/SubmitWJData_PF',
                contentType: "application/json", //必须有
                async: false,
                dataType: "json", //表示返回值类型，不必须
                    //data: JSON.stringify({ 'UserName': 'admin', 'UserPwd': '123456' }),  //相当于 //data: "{'str1':'foovalue', 'str2':'barvalue'}",
                data: JSON.stringify({
                    'PId': vId,
                    'WGJG': vWGJG,
                    'WGY':vWGY,
                    'WGN': vWGN,
                    'JYY':vJYY,
                    'JYYSFZH':vJYYSFZH,
                    'JCXDH':vJCXDH
                }),
                success: function(ret) {
                    if (ret.SubmitWJDataResult) {
                        if(ret.SubmitWJDataResult.IsSuccess)
                        {
                            RefreshWGHome();
                        }
                        else
                        {
                            api.alert({
                                title: '错误',
                                msg: ret.SubmitWJDataResult.Message,
                            }, function(ret, err){
                                $("#btnSaveWJ").removeClass("weui_btn_default");
                                $("#btnSaveWJ").addClass("weui_btn_primary");
                            });
                        }
                    } else {
                        api.closeWin();
                    }
                },
                error: function(data, status, e) {
                }
            });
        }
    }

    function RefreshWGHome()
    {
        api.openWin({
            name: 'win_wghome',
            url: 'win_wghome.html',
            bounces: false,
            delay: 200,
            reload:true,
        });

        api.closeWin({
            name: 'win_wgzplist',
            url: 'win_wgzplist.html',
        });
    }

    function ChangedItems()
    {
        var vItemsY = "";
        for(var i=0;i<chk_value_Y.length;i++)
        {
            vItemsY += chk_value_Y[i]+",";
        }

        $("#spItemsY").html(vItemsY);

        var vItemsN = "";
         for(var i=0;i<chk_value_N.length;i++)
        {
            vItemsN += chk_value_N[i]+",";
        }
        $("#spItemsN").html(vItemsN);
    }

    function AddN(obj)
    {    
        var isChecked = $(obj).is(':checked');
        if(isChecked)
        {
            var vSelected = $(obj).val();
            var indexN = $.inArray(vSelected, chk_value_N); 
            if (indexN < 0) {
                chk_value_N.push(vSelected); 
                delValInArr(vSelected,chk_value_Y);
            }
         }
         ChangedItems();
    }

    function AddY(obj)
    {
        var isChecked = $(obj).is(':checked');
         if(isChecked)
         {
            var vSelected = $(obj).val();
            var indexY = $.inArray(vSelected, chk_value_Y); 
            if (indexY < 0) {
                chk_value_Y.push(vSelected); 
                delValInArr(vSelected,chk_value_N);             
            }
         }
        ChangedItems();
    }

    function delValInArr(value,array){
           var pos=$.inArray(value,array);
           array.splice(pos,1);
    }
</script>